package fr.uparis10.miage.m1p1.config;

import java.io.File;

import java.util.ArrayList;



	
	import java.io.FileInputStream;
	import java.io.FileNotFoundException;
	import java.io.IOException;
	import java.text.SimpleDateFormat;
	import java.util.Date;
	import java.util.Properties;
	import java.util.logging.Level;
	import fr.uparis10.miage.m1p1.core.Projet;


	public class ProjetReader extends PropertyReader {
		private static ProjetReader theInstance = null;
		
		/**
		 * Private Constructor - singleton class
		 */private ProjetReader() {
			}
			
			public static ProjetReader getInstance() {
				if (theInstance == null) {
					theInstance = new ProjetReader(); 
				}
				return theInstance;
			}
			public ArrayList<Projet> readProjet(File aFile) {
				Properties prop = new Properties();
				FileInputStream inStream;
				
				ArrayList <Projet> projetListe = new ArrayList<Projet>();
				
//				HashMap<String, Departement> departementMap = Configurator.getInstance().getDepartementMap();
//				ArrayList<Departement> departementListe = Configurator.getInstance().getDepartementListe();
				
				try {
					inStream = new FileInputStream(aFile);
					prop.load(inStream);
				} catch (FileNotFoundException exc) {
					log.log(Level.SEVERE, "Exception occured: ", exc);
					return projetListe;
				} catch (IOException exc) {
					log.log(Level.SEVERE, "Exception occured: ", exc);
					return projetListe;
				}
				int projCount = getIntProperty(prop, "projets_count", -1);
				if (projCount <= 0) {
					log.log(Level.SEVERE, "Invalid projets_count=" + projCount + ". Return...");
					return projetListe;
				}
				SimpleDateFormat dateFormat;
				try {
					Object tmp = prop.get("date_format");
					dateFormat = new SimpleDateFormat(tmp.toString());
				} catch (NullPointerException exc) {
					log.log(Level.WARNING, "Unspecified date_format", exc);
					// default
					dateFormat = new SimpleDateFormat("dd/mm/yyyy");
				} catch (IllegalArgumentException exc) {
					log.log(Level.WARNING, "Invalid date_format", exc);
					// default
					dateFormat = new SimpleDateFormat("dd/mm/yyyy");
				}

				// lire les donn�es des employes
				for (int i = 0; i < projCount; i++) {
					
					/* lire le fichiers properties */
					
					
					String projnom = getStringProperty(prop, "proj_nom_" + i, "");
					Date projdateDebut = getDateProperty(prop, "proj_dateDebut_" + i, dateFormat);
					float projbudget= getFloatProperty(prop, "proj_budget_" + i, 0.0f);
					Date projdateFin = getDateProperty(prop, "proj_date_" + i, dateFormat);
					String[] projlangage = prop.getProperty("proj_langage_" + i, "").split(";");
					 //TODO: 
					// - creer un objet de type Projet
					ArrayList<String> projlangageList = new ArrayList<String>();
					for(String aName: projlangage) {
						projlangageList.add(aName);
					}

					Projet proj = new Projet(projnom, projlangageList, projbudget, 0.0d, projdateDebut, projdateFin);
					projetListe.add(proj);
				}
				return projetListe;
			}

			public static void main(String[] args) {
				
			}
	
}